﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Resources;

namespace JXT.PrimaryKey.Batman.Language
{
    /// <summary>
    /// 资源文件本地化实现
    /// </summary>
    public class ResourceLanguage : ILanguage
    {
        private readonly ResourceManager _resourceManager;

        public CultureInfo Culture { get; protected set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="resourceManager">资源管理器</param>
        /// <param name="culture">当前区域</param>
        public ResourceLanguage(ResourceManager resourceManager, CultureInfo culture)
        {
            _resourceManager = resourceManager;
            Culture = culture;
        }

        public ResourceLanguage(ResourceManager resourceManager)
        {
            _resourceManager = resourceManager;
            Culture = CultureInfo.CurrentCulture;
        }

        /// <summary>
        /// 获取所有转换对象
        /// </summary>
        /// <param name="culture">语言区域信息</param>
        /// <returns>转换对象列表</returns>
        public IEnumerable<Phrase> GetTranslations(CultureInfo culture)
        {
            var rs = _resourceManager.GetResourceSet(culture, true, true);
            IList<Phrase> list = new List<Phrase>();
            foreach (IDictionaryEnumerator item in rs)
            {
                list.Add(new Phrase()
                {
                    Culture = culture,
                    Key = Convert.ToString(item.Key),
                    Message = Convert.ToString(item.Value)
                });
            }
            return list;
        }

        /// <summary>
        /// 获取个性化文字
        /// </summary>
        /// <param name="key">资源key</param>
        /// <param name="culture">语言区域信息</param>
        /// <returns>对应区域文字</returns>
        public string Localize(string key, CultureInfo culture)
        {
            try
            {
                var value = _resourceManager.GetString(key, culture);
                if (String.IsNullOrEmpty(value))
                    return key;
                return value;
            }
            catch
            {
                return key;
            }
        }

        /// <summary>
        /// 获取个性化区域文字
        /// </summary>
        /// <param name="key">资源key</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="culture">语言区域信息</param>
        /// <returns>对应区域文字</returns>
        public string Localize(string key, string defaultValue, CultureInfo culture)
        {
            try
            {
                string value = _resourceManager.GetString(key, culture);
                if (String.IsNullOrEmpty(value))
                    return defaultValue;
                return value;
            }
            catch
            {
                return defaultValue;
            }
        }

        public IEnumerable<Phrase> GetTranslations()
        {
            return GetTranslations(Culture);
        }

        public string Localize(string key)
        {
            return Localize(key, Culture);
        }

        public string Localize(string key, string defaultValue)
        {
            return Localize(key, defaultValue, Culture);
        }
    }
}
